package org.carabiner.examples;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * A sample GUI control.
 * 
 * @author Ben Rady (benrady@gmail.com)
 */
public class MyGuiControl extends JPanel
{
  // This control's data is completely decoupled from the "view" using a
  // seperate data model
  private MyDataModel model;

  public MyGuiControl(MyDataModel dataModel)
  {
    // Notice how the constructor contains all of the
    // initialization code with no conditional logic. This structure is very
    // typical
    // of controls built and tested using Carabiner. This means that simply
    // constructing this class will result in 100% test coverage. Also notice
    // that this control is mostly just "glue" that holds together all the
    // individual elements of the control that actually do the work. In
    // practice,
    // this "glue" ends up creating the astethic elements of the control (size,
    // color, layout, etc...), thereby allowing us to encapsulate the astethic
    // elements in one class and test them independently.

    // root panel
    model = dataModel;
    setLayout(new BorderLayout());
    Font font = new Font("Arial", Font.PLAIN, 12);
    setFont(font);
    setPreferredSize(new Dimension(400, 400));
    setSize(new Dimension(400, 400));
    setMinimumSize(new Dimension(400, 400));
    setMaximumSize(new Dimension(400, 400));

    // Title Panel
    Container titlePanel = new JPanel(new FlowLayout());
    JLabel label = new JLabel("Today's Specials");
    font = new Font("Arial", Font.ITALIC, 20);
    label.setFont(font);
    titlePanel.add(label);
    add(titlePanel, BorderLayout.NORTH);

    // Center Panel
    Container centerPanel = new JPanel();
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
    centerPanel.add(new JLabel("Egg and Bacon"));
    centerPanel.add(Box.createVerticalStrut(10));
    centerPanel.add(new JLabel("Egg, Sausage and Bacon"));
    centerPanel.add(Box.createVerticalStrut(10));
    centerPanel.add(new JLabel("Egg, Bacon and Spam"));
    centerPanel.add(Box.createVerticalStrut(10));
    centerPanel.add(new JLabel("Egg, Bacon, Sausage and Spam"));
    centerPanel.add(Box.createVerticalStrut(10));
    centerPanel.add(new JLabel("Spam, Bacon, Sausage and Spam"));
    centerPanel.add(Box.createVerticalStrut(10));
    centerPanel.add(new JLabel("Spam, Egg, Spam, Spam, Bacon and Spam"));
    centerPanel.add(new JLabel());
    centerPanel.add(Box.createVerticalStrut(10));
    JLabel textLabel = new JLabel(model.getStringData());
    dataModel.addListDataListener(new DataModelListener(textLabel));
    centerPanel.add(textLabel);
    add(centerPanel, BorderLayout.CENTER);

    // South Panel
    Container southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    southPanel.add(new JButton("Vikings"));
    add(southPanel, BorderLayout.SOUTH);
  }
}
